const collect = require('../../models/good/collect');
class CollectService {
    static async getCollectList({ pageNum = 1, pageSize = 10 }, data) {
        try {
            const query = {
                userId: data.userId
            };
            const skip = (pageNum - 1) * pageSize;
            const limit = pageSize;
            const total = await collect.countDocuments(query);
            const collectList = await collect.find(query).skip(skip).limit(limit).populate("userId","-password").populate("goodId").lean();
            return {
                total,
                page: +pageNum,
                totalPages: Math.ceil(total / pageSize),
                list: collectList
            };
        } catch (error) {
            throw new Error('获取收藏列表失败: ' + error.message);
        }
    }

    static async createCollect(collectData) {
        return await collect.create(collectData);
    }

    static async getCollectByGoodId(goodId, userId) {
        return await collect.findOne({ goodId, userId });
    }


    static async deleteCollect(goodId, userId) {

        return await collect.findOneAndDelete({ goodId, userId });
    }



}
module.exports = CollectService;